package zmh.config;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.cache.caffeine.CaffeineCache;

import java.util.concurrent.TimeUnit;

/**
 * @author zhangminhang1
 * @date 2022-1-17 09:25:30
 */
public class CaffeineCacheFactoryBean implements FactoryBean<CaffeineCache> {
    @Override
    public CaffeineCache getObject() {
        System.out.println(this.getClass()+".getObject");
        Cache<Object,Object> objectCache = Caffeine.newBuilder().recordStats()
                .expireAfterWrite(5, TimeUnit.SECONDS)
                .maximumSize(1000)
                .build();
        return new CaffeineCache("caffeineCache",objectCache);
    }

    @Override
    public Class<?> getObjectType() {
        return CaffeineCache.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
